/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-07-28 16:45:35
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-09-18 22:08:32
 * @FilePath: /server/workspace/tests/test_iomanager.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"../src/base/Log.h"
#include"../src/base/iomanager.h"
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<fcntl.h>
#include "../src/base/timer.h"
Yizhi::Logger::ptr g_logger=SERVER_LOG_ROOT();
void test_fiber(){
    SERVER_LOG_DEBUG(g_logger)<<" test fiber";

    int sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock==-1){ 
       return;
    }
    fcntl(sock,F_SETFL,O_NONBLOCK);
    sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(80);
    inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr.s_addr);

    if(!connect(sock,(const sockaddr*)&addr,sizeof(addr))){

    }
    else if(errno==EINPROGRESS){
        SERVER_LOG_ERROR(g_logger)<<"add event error:"<<errno<<strerror(errno);

        Yizhi::IOManager::GetThis()->addEvent(sock,Yizhi::IOManager::Event::WRITE,[](){
           SERVER_LOG_DEBUG(g_logger)<<"write callback";
        });
    }
    else{
        SERVER_LOG_ERROR(g_logger)<<"else"<<errno<<strerror(errno);
    }
}

void test1(){
    Yizhi::IOManager iom;
    iom.scheduler(&test_fiber);
}
Yizhi::Timer::ptr s_timer;
void test2(){
    Yizhi::IOManager iom(2);
    Yizhi::Timer::ptr timer=iom.addTimer(1000,[&timer](){
        static int i=0;
        SERVER_LOG_DEBUG(g_logger)<<"hello timer:   "<<i;
        if(++i==3){
            timer->refush();
        }
    },true);
}
int main(int argc,char **argv){
    test2();
    return 0;
}